<?php

namespace App\Http\Controllers\MapOfYiqing;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Cache;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;

class MapOfYiqingController extends Controller
{
	
    public function feiyanIndex(){
    	$nxData = [];
    	$data = [];
    	$hasCache = 'noCache';
    	// Cache::flush();
    	if (Cache::has('data')) {
    		$hasCache = 'cache';
    		$data = Cache::get('data');
    	}else{
			header("Content-Type: text/html; charset=utf-8");  		
			// $data = file_get_contents ('http://api.tianapi.com/txapi/ncov/index?key=48e4b423d09ab455430087c4de318129' );
			
			$data = file_get_contents ('https://tianqiapi.com/api?version=epidemic&appid=17124671&appsecret=SvzUTGY9' );
			$data = json_decode($data, true);
		    if($data['errcode'] == 0){
		    	$data = $data['data'];
		    	Cache::put('data',$data,10);
		    }else{
		    	return view('feiyan/error');
		    }	
    	}
    	$date = $data['date'];//更新时间
    	$area = $data['area'];
    	// $area = json_decode($area, true);
    	foreach ($area as $province) {
    		if($province['locationId'] == '640000'){
    			$nxData = $province;
    		}
    	}
    	//各市确诊数据
    	$yc = 0;
    	$szs = 0;
    	$wz = 0;
    	$gy = 0;
    	$zw = 0;
    	$nd = 0;
    	foreach ($nxData['cities'] as $city) {
    		switch ($city['cityName']) {
    			case '银川':
    				$yc = $city['confirmedCount'];
    				break;
    			case '吴忠':
    				$wz = $city['confirmedCount'];
    				break;
    			case '固原':
    				$gy = $city['confirmedCount'];
    				break;
    			case '中卫':
    				$zw = $city['confirmedCount'];
    				break;
    			case '石嘴山':
    				$szs = $city['confirmedCount'];
    				break;
    			case '宁东管委会':
    				$nd = $city['confirmedCount'];
    				break;

    		}
    		# code...
    	}
    	$cities = ['yc'=>$yc,'szs'=>$szs,'wz'=>$wz,'gy'=>$gy,'zw'=>$zw,'nd'=>$nd];
    	// $cities = json_encode($cities,true);
    	return view('feiyan/index')->with(['data' => $nxData,'date' => $date,'cities' => $cities,'hasCache'=>$hasCache]);


    	

    }
    
}
